package com.totowoo.shirodemo.service.impl;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.totowoo.shirodemo.dao.UserDao;
import com.totowoo.shirodemo.model.User;
import com.totowoo.shirodemo.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;

	@Override
	public User getUserByUsername(String username) {
		return userDao.getUserByUsername(username);
	}

	@Override
	public void addUser(User user) {
		// 此处的密码应为md5后的密码
		// 为防止数据包拦截，前端向后端传递密码都需要经过加密后才能传递
		String password = user.getPassword();
		ByteSource byteSource = ByteSource.Util.bytes(password);
		ByteSource salt = ByteSource.Util.bytes(user.getUsername());
		Object encryptedPassword = new SimpleHash("MD5", byteSource, salt, 1024);
		user.setPassword(encryptedPassword.toString());
		
		userDao.addUser(user);
		
	}

}
